package com.hanock.sm.app;

import com.hanock.sm.sound.SoundManager;
import com.hanock.sm.sound.SoundType;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.provider.Settings.Secure;
import android.util.Log;

public class Application {

	
	private static Application instance = null;
	
	private Application(){
		
		initialized = false;
	};
	
	public static synchronized Application getInstance(){
		if (instance == null){
			instance = new Application();
		}
		return instance;
	}
	
	public static synchronized String getString(int resId){
		if (instance == null)
			return null;
		if (!instance.isInitialized() || instance.isReleased())
			return null;
		
		return instance.context.getString(resId);
	}
	
	public static synchronized String getString(int resId, Object... formatArgs){
		if (instance == null)
			return null;
		if (!instance.isInitialized() || instance.isReleased())
			return null;
		
		return instance.context.getString(resId, formatArgs);
	}
	
	public static synchronized void playSound(SoundType type){
		if (instance == null || type == null)
			return;
		if (!instance.isInitialized() || instance.isReleased())
			return;
		
		instance.getSoundManager().playSound(type);
	}
	
	public static synchronized void setLoopSoundVolume(SoundType type, float volume){
		
		if (instance == null || type == null)
			return;
		if (!instance.isInitialized() || instance.isReleased())
			return;
		
		instance.getSoundManager().setLoopSoundVolume(type, volume);
	}
	
	public static synchronized void playLoopSound(SoundType type){
		if (instance == null || type == null)
			return;
		if (!instance.isInitialized() || instance.isReleased())
			return;
		
		instance.getSoundManager().playLoopSound(type);
	}
	
	public static synchronized void pauseLoopSound(SoundType type){
		if (instance == null || type == null)
			return;
		if (!instance.isInitialized() || instance.isReleased())
			return;
		
		instance.getSoundManager().pauseLoopSound(type);
	}
	
	public static synchronized void pauseLoopSound(){
		if (instance == null)
			return;
		if (!instance.isInitialized() || instance.isReleased())
			return;
		
		instance.getSoundManager().pauseLoopSound();
	}
	
	public static synchronized void seekToBeginLoopSound(SoundType type){
		if (instance == null || type == null)
			return;
		if (!instance.isInitialized() || instance.isReleased())
			return;
		
		instance.getSoundManager().seekToBeginLoopSound(type);
	}
	
	 
	// ------------------------------------------------------------------------------------------------
	
	private boolean initialized;
	private boolean released;

	private SoundManager soundManager;
	
	private Context context;
	private Handler handler;
	
	private String versionName;
	private int versionCode;
	
	private String androidId;
	
	
	public synchronized void initialize(Context context, Handler handler){
		
		if (released)
			throw new RuntimeException();
		
		Log.i("test", "init app");
		
		if (initialized)
			return;
		
		this.handler = handler;
		this.context = context;
		
		
		try {
			PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
			this.versionName = info.versionName;
			this.versionCode = info.versionCode;
			
			Log.i("test2", "versionCode : " + versionCode);
			
		} catch (NameNotFoundException e) {
			e.printStackTrace();
			throw new RuntimeException("version load failed!!");
		}
		
		if (soundManager == null)
			soundManager = new SoundManager(context);
		
		androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
		
		initialized = true;
	}
	
	public synchronized void release(){
		
		if (released || !initialized)
			return;
		
		soundManager.release();
		released = true;
		
		Application.instance = null;
	}
	
	public synchronized boolean isInitialized(){
		return initialized;
	}
	
	public synchronized boolean isReleased() {
		return released;
	}
	
	public synchronized Context getContext() {
		return context;
	}
	
	public synchronized SoundManager getSoundManager() {
		return soundManager;
	}

	public synchronized Handler getHandler() {
		return handler;
	}
	
	public synchronized String getVersionName() {
		return versionName;
	}
	
	public synchronized int getVersionCode() {
		return versionCode;
	}

	public synchronized String getAndroidId() {
		return androidId;
	}
	
	// --------------------------------------------------------------------------------------------------------------------------

}


































